package com.lc.ibps.platform.rest.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lc.ibps.base.core.util.AppUtil;
import com.lc.ibps.base.core.util.BeanUtils;
import com.lc.ibps.base.core.util.JacksonUtil;
import com.lc.ibps.base.db.model.DefaultQueryFilter;
import com.lc.ibps.cloud.entity.APIResult;
import com.lc.ibps.common.cat.helper.AreaBuilder;
import com.lc.ibps.common.cat.persistence.entity.AreaPo;
import com.lc.ibps.common.cat.service.impl.DefaultAreaService;
import com.lc.ibps.web.controller.BaseApiController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 地理区域 控制类
 *
 * <pre>
 *  
 * 构建组：ibps-platform-webapi
 * 作者：lukui
 * 邮箱：625668769@qq.com
 * 日期：2018年5月17日-下午2:35:53
 * 版权：广州流辰信息技术有限公司版权所有
 * </pre>
 */
@RequestMapping(value = "/api/webapi/areaService")
@Api(value = "/areaService", tags = "系统管理-地理区域服务")
@RestController
public class AreaRepositoryController extends BaseApiController {

	private DefaultAreaService defAreaService;

	public AreaRepositoryController() {
		defAreaService = AppUtil.getBean(DefaultAreaService.class);
	}

	@ApiOperation(value = "获取地理区域", notes = "按级别分类")
	@RequestMapping(value = "/getPickerData", method = RequestMethod.GET)
	public APIResult<Map<String, Object>> getPickerData() {
		APIResult<Map<String, Object>> result = new APIResult<>();
		DefaultQueryFilter queryFilter = new DefaultQueryFilter();
		queryFilter.setPage(null);
		List<AreaPo> areaPoList = null;
		try {
			String listData = defAreaService.query(queryFilter);
			if (JacksonUtil.isJsonArray(listData)) {
				areaPoList = JacksonUtil.getDTOList(listData, AreaPo.class);
			}

			Map<String, Map<String, String>> pickerData = new HashMap<>();
			if (BeanUtils.isNotEmpty(areaPoList)) {
				pickerData = AreaBuilder.buildByType(areaPoList);
			}
			Map<String, Object> map = new HashMap<String, Object>();
			for (String key : pickerData.keySet()) {
				map.put(key, pickerData.get(key)!=null?pickerData.get(key):"");
			}
			result.setData(map);
			result.setMessage("获取地区区域成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取地区区域失败！", e);
		}
		return result;
	}

	@ApiOperation(value = "获取地理区域", notes = "按字母分类")
	@RequestMapping(value = "/getData", method = RequestMethod.GET)
	public APIResult<Map<String, Object>> getData() {
		APIResult<Map<String, Object>> result = new APIResult<>();
		DefaultQueryFilter queryFilter = new DefaultQueryFilter();
		queryFilter.setPage(null);
		List<AreaPo> areaPoList = null;
		try {
			String listData = defAreaService.query(queryFilter);
			if (JacksonUtil.isJsonArray(listData)) {
				areaPoList = JacksonUtil.getDTOList(listData, AreaPo.class);
			}

			Map<String, Object> pickerData = new HashMap<>();
			if (BeanUtils.isNotEmpty(areaPoList)) {
				pickerData = AreaBuilder.buildPickerData(areaPoList);
			}
			result.setData(pickerData);
			result.setMessage("获取地区区域成功！");
		} catch (Exception e) {
			result = getExceptionResult(result, ERROR, "获取地区区域失败！", e);
		}
		return result;
	}

	// /**
	// * 按省获取地理区域
	// */
	// @ApiOperation(value = "获取地理区域", notes = "按省份获取区域")
	// @RequestMapping(value="/getAreaByProvince",method=RequestMethod.GET)
	// public WebAPIResult getAreaByProvince(){
	// WebAPIResult result = new WebAPIResult();
	// List<AreaPo>
	// adress=defAreaService.getByCondition(AreaPo.ProvinceStartKey,
	// AreaPo.ProvinceEndKey);
	// Map<Integer, String> pm = new HashMap<Integer, String>();
	// for(int i=0;i<adress.size();i++){
	// Integer key=Integer.parseInt(adress.get(i).getKey());
	// pm.put(key, adress.get(i).getName());
	// }
	// result.setData(pm);
	// result.setResult(WebAPIResult.SUCCESS);
	// result.setMessage("获取省份区域成功！");
	// return result;
	// }
	//
	// /**
	// *
	// * 按市区获取地理区域
	// *
	// * @return
	// */
	// @ApiOperation(value = "获取地理区域", notes = "按市区获取区域")
	// @RequestMapping(value="/getAreaByCity",method=RequestMethod.GET)
	// public WebAPIResult getAreaByCity(){
	// WebAPIResult result = new WebAPIResult();
	// List<AreaPo> adress=defAreaService.getByCondition(AreaPo.CityStartKey,
	// AreaPo.CityEndKey);
	// Map<Integer, String> pm = new HashMap<Integer, String>();
	// for(int i=0;i<adress.size();i++){
	// Integer key=Integer.parseInt(adress.get(i).getKey());
	// pm.put(key, adress.get(i).getName());
	// }
	// result.setData(pm);
	// result.setResult(WebAPIResult.SUCCESS);
	// result.setMessage("获取市区区域成功！");
	// return result;
	// }
	//
	// @ApiOperation(value = "获取地理区域", notes = "按地区获取区域")
	// @RequestMapping(value="/getAreaByCounty",method=RequestMethod.GET)
	// public WebAPIResult getAreaByCounty(){
	// WebAPIResult result = new WebAPIResult();
	// List<AreaPo> adress=defAreaService.getByCondition(AreaPo.CountyStartKey,
	// AreaPo.CountyEndKey);
	// Map<Integer, String> pm = new HashMap<Integer, String>();
	// for(int i=0;i<adress.size();i++){
	// Integer key=Integer.parseInt(adress.get(i).getKey());
	// pm.put(key, adress.get(i).getName());
	// }
	// result.setData(pm);
	// result.setResult(WebAPIResult.SUCCESS);
	// result.setMessage("获取地区区域成功！");
	// return result;
	// }

}
